package com.tanhua.admin.listener;

import com.alibaba.fastjson.JSON;
import com.tan.model.domain.Log;
import com.tanhua.admin.mapper.LogMapper;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class LogLitener {
    @Autowired
    private LogMapper logMapper;
    @RabbitListener(

            bindings = @QueueBinding(
                    value = @Queue(value = "tanhua.log.queue",durable = "true"),
                    exchange = @Exchange(value = "tanhua.log.exchange",type = ExchangeTypes.TOPIC),
                    key = "log.*"
            )

    )
    public void log(String message) {
      Map map =  JSON.parseObject(message, Map.class);
        //1.解析MAP获取数据
      Long userId = Long.valueOf(map.get("userId").toString());
      String type = map.get("type").toString();
      String logTime=(String)map.get("logTime");
      //2.构造log对象，调用mapper保存到数据库中
        Log log = new Log(userId,type,logTime);
        logMapper.insert(log);




    }

}
